package cn.zhaoyuening.leasecloud.middle.controller;

import cn.zhaoyuening.leasecloud.middle.phpinterface.DeviceControlInterface;
import hprose.server.HproseHttpMethods;
import hprose.server.HproseHttpService;
import hprose.server.HttpContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.ServletConfigAware;
import org.springframework.web.context.ServletContextAware;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by Buynow on 2017/7/1.
 * 向php暴露调用接口
 */
@RestController
@RequestMapping("rpc")
public class RpcController implements ServletConfigAware,ServletContextAware {
    @Autowired
    private HproseHttpService httpService;

    private ServletConfig servletConfig;
    private ServletContext servletContext;

    @Autowired
    private DeviceControlInterface deviceTypeInterface;

    @RequestMapping(value = "deviceTypeService")
    public void deviceTypeService(HttpServletRequest request, HttpServletResponse response) {
        HproseHttpMethods methods = new HproseHttpMethods();
        methods.addInstanceMethods(deviceTypeInterface);
        methods.addInstanceMethods(deviceTypeInterface, DeviceControlInterface.class);
        httpService.handle(new HttpContext(httpService,request, response, servletConfig,servletContext), methods);
    }

    @Override
    public void setServletConfig(ServletConfig servletConfig) {

        this.servletConfig = servletConfig;
    }

    @Override
    public void setServletContext(ServletContext servletContext) {

        this.servletContext = servletContext;
    }
}
